package uit.jee.lib.access.mapper;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import uit.jee.lib.dto.AuthorDTO;
import uit.jee.lib.dto.BookDTO;

/**
 *
 * @author ngloc_it
 */
public class AuthorMapper extends MapperDB{
    public AuthorMapper() throws Exception{
        super();
    }

    private void InitAuthorDTOFromRs(AuthorDTO author, ResultSet rs) throws Exception{
        if((rs != null) && (author != null)){
            author.setAddress(rs.getString(AuthorDTO.AUTHOR_ADDRESS));
            author.setEmail(rs.getString(AuthorDTO.AUTHOR_EMAIL));
            author.setId(rs.getString(AuthorDTO.AUTHOR_ID));
            author.setName(rs.getString(AuthorDTO.AUTHOR_NAME));
            author.setPhoneNumber(rs.getString(AuthorDTO.AUTHOR_PHONE));
            author.setProfile(rs.getString(AuthorDTO.AUTHOR_PROFILE));
        }
    }

    public ArrayList<AuthorDTO> GetAllAuthor() throws Exception{
        ArrayList<AuthorDTO> authors = new ArrayList<AuthorDTO>();
        try {
            StringBuffer sql = new StringBuffer();
            sql.append("select * from booklibrary.tacgia");
            PreparedStatement stmt = getConnection().prepareStatement(sql.toString());
            ResultSet re = stmt.executeQuery();
            while (re != null && re.next()) {
                AuthorDTO author = new AuthorDTO();
                InitAuthorDTOFromRs(author, re);
                authors.add(author);
            }
        } catch (Exception ex) {
            throw ex;
        }
        return authors;
    }

    public AuthorDTO GetAuthorByName(String name) throws Exception{
        AuthorDTO author = new AuthorDTO();
        try {
            StringBuffer sql = new StringBuffer();
            sql.append("select * from booklibrary.tacgia where ");
            sql.append(AuthorDTO.AUTHOR_NAME).append("= '").append(name).append("'");

            PreparedStatement stmt = getConnection().prepareStatement(sql.toString());
            ResultSet re = stmt.executeQuery();
            if (re != null && re.next()) {
                InitAuthorDTOFromRs(author, re);
                return author;
            }
        } catch (Exception ex) {
            throw ex;
        }
        return null;
    }

    public AuthorDTO GetAuthorById(String id) throws Exception{
        AuthorDTO author = new AuthorDTO();
        try {
            StringBuffer sql = new StringBuffer();
            sql.append("select * from booklibrary.tacgia where ");
            sql.append(AuthorDTO.AUTHOR_ID).append("= '").append(id).append("'");

            PreparedStatement stmt = getConnection().prepareStatement(sql.toString());
            ResultSet re = stmt.executeQuery();
            if (re != null && re.next()) {
                InitAuthorDTOFromRs(author, re);
                return author;
            }
        } catch (Exception ex) {
            throw ex;
        }
        return null;
    }

    public AuthorDTO GetAuthor(String bookId) throws Exception{
        AuthorDTO author = new AuthorDTO();
        try {
            StringBuffer sql = new StringBuffer();
            sql.append("select * from booklibrary.tacgia, booklibrary.sach  where booklibrary.tacgia.MaTacGia = booklibrary.sach.MaTacGia  and ");
            sql.append("booklibrary.sach.MaSach ='").append(bookId).append("'");

            PreparedStatement stmt = getConnection().prepareStatement(sql.toString());
            ResultSet re = stmt.executeQuery();
            if (re != null && re.next()) {
                InitAuthorDTOFromRs(author, re);
                return author;
            }
        } catch (Exception ex) {
            throw ex;
        }
        return null;
    }

    public AuthorDTO GetAuthor(BookDTO book) throws Exception{
        AuthorDTO author = new AuthorDTO();
        try {
            StringBuffer sql = new StringBuffer();
            sql.append("select * from booklibrary.tacgia, booklibrary.sach  where booklibrary.tacgia.MaTacGia = booklibrary.sach.MaTacGia  and ");
            sql.append("booklibrary.sach.MaSach ='").append(book.getBookId()).append("'");

            PreparedStatement stmt = getConnection().prepareStatement(sql.toString());
            ResultSet re = stmt.executeQuery();
            if (re != null && re.next()) {
                InitAuthorDTOFromRs(author, re);
                return author;
            }
        } catch (Exception ex) {
            throw ex;
        }
        return null;

    }
}
